package com.campus.controller;

import com.campus.model.CourseLearnUnit;


import com.campus.service.CourseLearnUnitService;
import com.campus.web.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/homework/unit")
public class UnitController {

    @Autowired
    private CourseLearnUnitService courseLearnUnitService;

    @PostMapping("/create")
    public ResponseResult<String> createCourseLearnUnit(@RequestBody CourseLearnUnit unit) {
        boolean success = courseLearnUnitService.createCourseLearnUnitWithStudents(unit);
        return success
                ? ResponseResult.ok("Course learn unit created and students linked successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to create course learn unit");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult<String> deleteCourseLearnUnit(@PathVariable int id) {
        boolean success = courseLearnUnitService.deleteCourseLearnUnit(id);
        return success
                ? ResponseResult.ok("Course learn unit deleted successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to delete course learn unit");
    }

    @PutMapping("/update")
    public ResponseResult<String> updateCourseLearnUnit(@RequestBody CourseLearnUnit unit) {
        boolean success = courseLearnUnitService.updateCourseLearnUnit(unit);
        return success
                ? ResponseResult.ok("Course learn unit updated successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to update course learn unit");
    }

    @GetMapping("/list")
    public ResponseResult<List<CourseLearnUnit>> listAllCourseLearnUnits() {
        List<CourseLearnUnit> units = courseLearnUnitService.listAllCourseLearnUnits();
        return units != null && !units.isEmpty()
                ? ResponseResult.ok(units)
                : ResponseResult.error(HttpStatus.NOT_FOUND, "No course learn units found");
    }

}
